$(function(){
	getAjBank();
});

//获取按揭银行数据
function getAjBank(){
	var BugUID = $("#BugUID").val();
	$.ajax({
		url: ctx+"/project/payment/getBankList",
		type:'POST',
		dataType: "json",
		data:{ 
			BugUID:BugUID
		},
		success:function(data){
			if(data.success){
				htmlOper='';
				htmlOper += "<option value=''></option>";
				$.each(data.bankList,function(index, item){ 
					htmlOper += "<option value='"+item.BankName+"' onclick='getAjBankInfo()'>"+item.BankName+"</option>";
				});
				$("#AjBank").html(htmlOper);
				$("#GjjBank").html(htmlOper);
			}
		},
		error:function(){
		}
	});
}

//产品类型弹框
function showProductTypeDialog() {
	var productDialog = null;
	var ProjGUID = $("#ProjGUID").val();
	var echoCategoryId = $("#BProductTypeIds").val();
	if(productDialog == null){
		productDialog = new ProductTypeDialog({
			title:"产品类型选择",      //弹出框标题
			echoCategoryId: echoCategoryId,
			requestParams: {
				ProjGUID:ProjGUID
			},
			okCallback:function(selectNode,ztreeObj){
				saveCategoryInfo(selectNode);
				console.log("ok-callback:",selectNode,ztreeObj);
			},
			cancelCallback:function(){
				console.log("cancel-callback");
			}, 
			beforeOkCallback:function(selectNode,ztreeObj){
				return true;
			}
	    });
		productDialog.show();
	}
}

/**
 * 选择完资产分类后的回调函数
 * @param AssetClassification
 */
function saveCategoryInfo(selectNodes) {
	var categoryIds="";
	var categoryNames="";
	var levels="";
	$.each(selectNodes,function(i,item){
		var id=item.id;
		var name=item.name;
		var level=item.level;
		if(level !='0'){
			categoryIds+=id+",";
			categoryNames+=name+",";
			levels+=level+",";
		}
	});
	categoryIds=categoryIds.substring(0, categoryIds.length - 1);
	categoryNames=categoryNames.substring(0, categoryNames.length - 1);
	levels=levels.substring(0, levels.length - 1);
	$("#BProductTypeIds").val(categoryIds);
	$("#BProductTypeShortName").val(categoryNames);
	$("#categoryLevel").val(levels);
}

function getAjBankInfo(){
	var selectedInfo = $('#AjBank option:selected').text();
	$("#AjBank").val(selectedInfo);
}

//新增保存
function addPayform(){
	var projGUID = $("#ProjGUID").val();
	var payformName = $("#payformName").val();
	var payFormType = $('#payFormType option:selected').text();
	var bgnDate = $("#bgnDate").val();
	if(isEmpty(bgnDate)){
		$("#bgnDateP").removeClass("none");
		$("#bgnDateP").text("请选择生效日期!");
		return false;
	};
	var endDate = $("#endDate").val();
	if(isEmpty(endDate)){
		$("#endDateP").removeClass("none");
		$("#endDateP").text("请选择失效日期!");
		return false;
	};
	if(bgnDate > endDate){
		showAlertDialog({msg:'生效时间大于失效时间！'});
		return false;
	};
	var AjBank = $('#AjBank option:selected').text();
	if(!isEmpty(AjBank)){
		var AjZero = $('#AjZero option:selected').text();
	}
	var GjjBank = $('#GjjBank option:selected').text();
	if(!isEmpty(GjjBank)){
		var GjjZero = $('#GjjZero option:selected').text();
	}
	var BProductTypeShortName = $("#BProductTypeShortName").val();
	if(isEmpty(BProductTypeShortNameP)){
		$("#BProductTypeShortNameP").removeClass("none");
		$("#BProductTypeShortNameP").text("请选择生效日期!");
		return false;
	};
	var payFormScope = $('#payFormScope option:selected').text();
	
	$.ajax({
		url: ctx+"/project/payment/increase",
		type:'POST',
		dataType: "json",
		data:{ 
			projGUID:projGUID,
			payformName:payformName,
			payFormType:payFormType,
			bgnDate:bgnDate,
			endDate:endDate,
			AjBank:AjBank,
			AjZero:AjZero,
			GjjBank:GjjBank,
			GjjZero:GjjZero,
			BProductTypeShortName:BProductTypeShortName,
			payFormScope:payFormScope
		},
		success:function(data){
			if(data.success){
				showAlertDialog({msg:'保存成功'});
				toPayformList();
			}
		},
		error:function(){
			showAlertDialog({msg:'新建失败'});
		}
	});
}

//跳转列表页
function toPayformList(){
	window.open(ctx+"/project/payment/list");
}

/**
 * @Title: removeErroInfo
 * @Description: TODO 去除错误提醒
 */
function removeErroInfo(erroId) {
	if(!$("#"+erroId).hasClass("none")) {
		$("#"+erroId).addClass("none");
	}
}

/**
 * @Title: isEmpty
 * @Description: 验证主要必填内容是否为空
 */
function isEmpty(pointContent){
	if(pointContent == null || pointContent == undefined || pointContent.length <= 0 ||
			$.trim(pointContent).length <= 0 || pointContent == '无') return true;		
	return false;
}